<?php
	session_start();
	include_once('init.php');
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title>PHP 操作 mongodb 数据库 by 此题无解</title>
		<meta charset="UTF-8">
		<meta name=description content="">
		<meta name=viewport content="width=device-width, initial-scale=1">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<!-- Bootstrap CSS -->
		<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
		<link href="./css/bootstrap.min.css" rel="stylesheet" media="screen">
	</head>
	<body>
	<h1 class="text-center">PHP 操作 mongodb 数据库</h1>
	<div class="container">
		<div class="row">
		1. 连接MongoDB
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'connect.txt');
			echo $s;
		?>
		</pre>
		<?php 
			$mo = new Mon($CONFIG['MONGDB']);
			if (!$mo->isConnect) {
				$result = "连接数据库失败,".$mo->errorInfo;
			} else {
				$result = "连接数据库成功!";
			}
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?=$result?></pre>
		</div>		

		2. 查看所有数据库
		<pre class="prettyprint linenums Lang-php">
		<?php
			$s = file_get_contents(CODE.'listdbs.txt');
			echo $s;
		?>
		</pre>
		<?php 
			if (!$mo->isConnect) {
				$result = "连接数据库失败,".$mo->errorInfo;
			} else {
				$result = $mo->listDBs();
			}
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);?></pre>
		</div>		


		3. 选择数据库
		<pre class="prettyprint linenums Lang-php">
			<?php
			$s = file_get_contents(CODE.'selectdb.txt');
			echo $s;
		?>
		</pre>
		<?php 
			if (!$mo->isConnect) {
				$result = "连接数据库失败,".$mo->errorInfo;
			} else {
				$db = $mo->selectDB('foobar');
				$result = $db?'选择foobar成功':'选择foobar失败';

			}
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?=$result?></pre>
		</div>

		4. 选择集合
		<pre class="prettyprint linenums Lang-php">
			<?php
			$s = file_get_contents(CODE.'collection.txt');
			echo $s;
			?>
		</pre>
		<?php 
			if (!$mo->isConnect) {
				$result = "连接数据库失败,".$mo->errorInfo;
			} else {
				$collection= $mo->selectCollection('study')?'选择foobar成功':'选择foobar失败';
				$result = $collection?'选择study成功':'选择study失败';
			}
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?=$result?></pre>
		</div>

		5. 清空数据
		<pre class="prettyprint linenums Lang-php">
			<?php
			$s = file_get_contents(CODE.'remove.txt');
			echo $s;
			?>
		</pre>
		<?php 
			if (!$mo->isConnect) {
				$result = "连接数据库失败,".$mo->errorInfo;
			} else {
				$insertResult = $mo->remove(); 
				$result = $insertResult?'清空成功':'清空失败';
			}
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?=$result?></pre>
		</div>

		6. 插入数据
		<pre class="prettyprint linenums Lang-php">
			<?php
			$s = file_get_contents(CODE.'insert.txt');
			echo $s;
			?>
		</pre>
		<?php 
			if (!$mo->isConnect) {
				$result = "连接数据库失败,".$mo->errorInfo;
			} else {
				$data0 = array(
						'name'=>'lisi',
						'age'=>18,
						'sex'=>'man',);
				$data1 =array(
						'name'=>'lisi',
						'age'=>rand(0,9),
						'sex'=>'man',
					);
				$insertResult = $mo->insert($data0);
				$insertResult = $mo->insert($data1); 
				$result = $insertResult?'插入成功':'插入失败';
			}
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?=$result?></pre>
		</div>

		7. 查找所有数据
		<pre class="prettyprint linenums Lang-php">
			<?php
			$s = file_get_contents(CODE.'find.txt');
			echo $s;
			?>
		</pre>
		<?php 
			if (!$mo->isConnect) {
				$result = "连接数据库失败,".$mo->errorInfo;
			} else {
				$result = array();
				$cursor = $mo->find();
				if ($cursor) {
					foreach ($cursor as $key => $value) {
						$result[] = $value;
					}
				} else $result = "查找失败";
			}
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);?></pre>
		</div>

		8. 修改数据(更新)
		<pre class="prettyprint linenums Lang-php">
			<?php
			$s = file_get_contents(CODE.'update1.txt');
			echo $s;
			?>
		</pre>
		<?php 
			if (!$mo->isConnect) {
				$result = "连接数据库失败,".$mo->errorInfo;
			} else {
				$dataOld = array('age'=>18,);
				$dataNew =array('name'=>'TAT',);
				$updateResult = $mo->update($dataOld,array('$set'=>$dataNew));
				$result = array();
				$cursor = $mo->find();
				if ($cursor) {
					foreach ($cursor as $key => $value) {
						$result[] = $value;
					}
				} else $result = "查找失败";
			}

		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);?></pre>
		</div>

		9. 删除指定数据节点
		<pre class="prettyprint linenums Lang-php">
			<?php
			$s = file_get_contents(CODE.'update2.txt');
			echo $s;
			?>
		</pre>
		<?php 
			if (!$mo->isConnect) {
				$result = "连接数据库失败,".$mo->errorInfo;
			} else {
				$result = array();
				$condition = array('age'=>18);
				$mo->update($condition,array('$unset'=>'sex'));  
				$cursor = $mo->find();
				if ($cursor) {
					foreach ($cursor as $key => $value) {
						$result[] = $value;
					}
				} else $result = "查找失败";
			}
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);?></pre>
		</div>

		10. 修改数据(替换)
		<pre class="prettyprint linenums Lang-php">
			<?php
			$s = file_get_contents(CODE.'update.txt');
			echo $s;
			?>
		</pre>
		<?php 
			if (!$mo->isConnect) {
				$result = "连接数据库失败,".$mo->errorInfo;
			} else {
				$dataOld = array('age'=>18,);
				$dataNew =array('name'=>'TAT',);
				$updateResult = $mo->update($dataOld,$dataNew);
				$result = array();
				$cursor = $mo->find();
				if ($cursor) {
					foreach ($cursor as $key => $value) {
						$result[] = $value;
					}
				} else $result = "查找失败";
			}
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);?></pre>
		</div>

		

		11. 查找指定数据
		<pre class="prettyprint linenums Lang-php">
			<?php
			$s = file_get_contents(CODE.'finds.txt');
			echo $s;
			?>
		</pre>
		<?php 
			if (!$mo->isConnect) {
				$result = "连接数据库失败,".$mo->errorInfo;
			} else {
				$result = array();
				$condition = array('age'=>18);
				$cursor = $mo->find($condition);
				if ($cursor) {
					$result[] = '找到'.$cursor->count().'条数据';
					foreach ($cursor as $key => $value) {
						$result[] = $value;
					}
				} else $result = "查找失败";
			}
		?>
		<div class="alert">
			<a href="#" class="close" data-dismiss="alert">&times;</a>
			<strong>输出: </strong>
			<pre><?php var_dump($result);?></pre>
		</div>

		<div><blockquote><em>php</em>操作<em>mongodb</em>数据库入门篇(1)<br>
		<a href="second.php">下一篇</a>
		<small class="pull-right"><cite>此题无解</cite></small></blockquote></div>

		</div>
	</div>

		<!-- jQuery -->
		<script src="./js/jquery.min.js"></script>
		<!-- Bootstrap JavaScript -->
		<script src="./js/bootstrap.min.js"></script>
		<!-- prettify -->
		<script src="./js/prettify.js"></script>
		<script>
		$(function(){
			prettyPrint();
		});
		</script>
	</body>
</html>